package com.naicheng.ivote.views;
import java.util.HashMap;

import org.json.JSONObject;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.naicheng.ivote.MyApplication;
import com.naicheng.ivote.R;
import com.naicheng.ivote.doman.BaseResponseEntity;
import com.naicheng.ivote.doman.UserInfo;
import com.naicheng.ivote.engin.BaseActivity;
import com.naicheng.ivote.parser.BaseParser;
import com.naicheng.ivote.utils.AppManager;
import com.naicheng.ivote.utils.MyPreferences;
import com.naicheng.ivote.volley.Response.Listener;


public class LoginActivity extends BaseActivity{

	LoginActivity CTLA=LoginActivity.this;
	private MyPreferences preference;
	AppManager appManager = AppManager.getAppManager();
	
	private TextView login_title_line;
	private TextView edit_short_line1;
	private TextView edit_short_line2;
	private EditText login_user_et;
	private EditText login_password_et;
	
	
	@Override
	protected void setConentView() {
		super.setConentView();
		setContentView(R.layout.activity_login);
	}

	@Override
	protected void init() {
		super.init();
		login_user_et=(EditText) findViewById(R.id.login_user_et);
		login_password_et=(EditText) findViewById(R.id.login_password_et);
		
		login_password_et.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if(s.toString().trim().length()>=6){
					login();
				}
			}
		});
		
		
	}

	@Override
	protected void processLogic() {
		super.processLogic();
	}

	@Override
	protected void setOnClickListener() {
		// TODO Auto-generated method stub
		super.setOnClickListener();
	}
	
	
	private void login(){
		
		String username = login_user_et.getText().toString().trim();
		String userpwd = login_password_et.getText().toString().trim();
		
		if(TextUtils.isEmpty(username)){
			Toast.makeText(this, "请输入用户名", Toast.LENGTH_LONG).show();
			return;
		}
		if(TextUtils.isEmpty(userpwd)){
			Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
			return;
		}
		
		HashMap<String,String> map = new HashMap<String, String>();
		map.put("account", username);
		map.put("password", userpwd);
		
		Listener<JSONObject> listener = new Listener<JSONObject>() {

			@Override
			public void onResponse(JSONObject response) {
				System.out.println("response--->"+response.toString());
				BaseParser<UserInfo> parser = new BaseParser<UserInfo>();
				BaseResponseEntity<UserInfo> mResponse = parser.parserDomain(response, UserInfo.class);
				if(mResponse!=null){
					if(mResponse.getState()){
						MyApplication.user = mResponse.getSingleDomain();
						Toast.makeText(LoginActivity.this, "登录成功",Toast.LENGTH_LONG).show();
						AppManager.getAppManager().finishActivity();
					}else{
						Toast.makeText(LoginActivity.this, mResponse.getMsg(),Toast.LENGTH_LONG).show();
					}
				}
				
			}
		};
		
		super.getDataFromServer("login", map, listener, this);
	}

	

	

}
